package com.ruge.gateway.controller;

import com.ruge.gateway.config.MySwaggerResourcesProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.List;
import java.util.Optional;

/**
 * swagger聚合接口，三个接口都是swagger-ui.html需要访问的接口
 * @author ruge.wu
 * @since 2021/12/14 12:45
 */
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {
    @Autowired(required = false)
    private SecurityConfiguration securityConfiguration;

    @Autowired(required = false)
    private UiConfiguration uiConfiguration;

    private final MySwaggerResourcesProvider swaggerResources;

    @Autowired
    public SwaggerResourceController(MySwaggerResourcesProvider swaggerResources) {
        this.swaggerResources = swaggerResources;
    }

    @GetMapping(value = "/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(this.securityConfiguration)
                        .orElseGet(() -> SecurityConfigurationBuilder.builder().build()),
                HttpStatus.OK));
    }

    @GetMapping(value = "/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono
                .just(new ResponseEntity<>(
                        Optional.ofNullable(this.uiConfiguration).orElseGet(
                                () -> UiConfigurationBuilder.builder().build()),
                        HttpStatus.OK));
    }

    @GetMapping
    public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() {
        return Mono
                .just(new ResponseEntity<>(this.swaggerResources.get(), HttpStatus.OK));
    }
}
